package com.liaoyifan.core.valid.validator;

import com.liaoyifan.core.valid.constraints.IdCard;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

@SuppressWarnings("unused")
public class IdCardValidator implements ConstraintValidator<IdCard, String> {

    private static final Pattern PATTERN = Pattern.compile("(^\\d{15}$)|(^\\d{17}([0-9]|X|x)$)");

    @Override
    public boolean isValid(String idCard, ConstraintValidatorContext context) {
        if (idCard == null) return true;
        return PATTERN.matcher(idCard).matches();
    }
}
